﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FCS.Emulator;
using System.Net;
using System.Windows.Controls;
using FCS.GlobalComponents;

namespace FCS.Emulator
{
    public static class EmulatorUIManager
    {
        public static ERIO _erio;
        public static ConnectionData _connectionData = new ConnectionData();
        private static AsyncCallback _callBack = new AsyncCallback(HandlePacket);
        private static IAsyncResult _aSyncResult;
        private static UIObjects _uiObjects;

        public static void StartEmulator(UIObjects uiObjects)
        {
            // Create the ERIO, used to handle communications and parsing of packets
            _erio = new ERIO(_connectionData.ipa, _connectionData.emulatorListenPort, _connectionData.serverListenPort);
            // Grab the references to the interface objects
            _uiObjects = uiObjects;
            // Begin listening for packets
            _aSyncResult = _erio.ListenAsync(_callBack);
        }

        public static void DisconnectEmulator()
        {
            _erio.Disconnect();
        }

        public static void HandlePacket(IAsyncResult iar)
        {
            // End the receive operation and get the packet
            byte[] receivedPacket = _erio.EndListenAsync(_aSyncResult);

            // Parse into a new packet
            FountainState fs = _erio.ParseToFountainState(receivedPacket);

            // Display this data on the UI
            for (int i = 0; i < FountainConstants.SouthEndValveCountPerCaliper; i++)
            {
                _uiObjects.HV[i].SetState(fs._horizontalValveStates[i]);
            }
            for (int i = 0; i < FountainConstants.SouthEndValveCountPerCaliper; i++)
            {
                _uiObjects.VV[i].SetState(fs._verticalValveStates[i]);
            }
            for (int i = 0; i < FountainConstants.NorthEndValveCount; i++)
            {
                _uiObjects.northEnd[i].SetState(fs._northValveStates[i]);
            }
            for (int i = 0; i < FountainConstants.LightCount; i++)
            {
                _uiObjects.lights[i].SetState(fs._lightStates[i]);
            }
            for (int i = 0; i < FountainConstants.WeirCount; i++)
            {
                _uiObjects.weirs[i].SetState(fs._weirStates[i]);
            }
            _uiObjects.northMister.SetState(fs._northMisterState);
            _uiObjects.southMister.SetState(fs._southMisterState);
            _uiObjects.northValveState.SetState(fs._northValveState);

            // Grab data from the UI
            for (int i = 0; i < FountainConstants.PumpCount; i++)
            {
                fs._pumpStates[i] = _uiObjects.pumps[i].GetState();
            }
            for (int i = 0; i < FountainConstants.PumpCount; i++)
            {
                fs._pumpVacuumStates[i] = _uiObjects.pumpVacuums[i].GetState();
            }
            for (int i = 0; i < FountainConstants.BollardCount; i++)
            {
                fs._bollardStates[i] = _uiObjects.bollards[i].GetState();
            }
            for (int i = 0; i < FountainConstants.ReportedEnvironmentalStates; i++)
            {
                fs._reportedEnvironStates[i] = _uiObjects.environ[i].GetState();
            }
            fs._chassisTemperature = _uiObjects.temperature.GetState();


            // Set this as the ERIO's internal state
            _erio.SetState(fs);

            // ERIO needs to send a status packet;
            _erio.SendStatus();
           
        }
    }
}
